<?php
/**
 *
 * OrangeHRM is a comprehensive Human Resource Management (HRM) System that captures
 * all the essential functionalities required for any enterprise.
 * Copyright (C) 2006 OrangeHRM Inc., http://www.orangehrm.com
 *
 * OrangeHRM is free software; you can redistribute it and/or modify it under the terms of
 * the GNU General Public License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * OrangeHRM is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with this program;
 * if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA  02110-1301, USA
 *
 * @copyright 2006 OrangeHRM Inc., http://www.orangehrm.com
 */

require_once ROOT_PATH . '/lib/common/htmlMimeMail5/htmlMimeMail5.php';
require_once ROOT_PATH . '/lib/models/eimadmin/EmailConfiguration.php';
require_once ROOT_PATH . '/lib/models/eimadmin/EmailNotificationConfiguration.php';
require_once ROOT_PATH . '/lib/confs/sysConf.php';


require_once ROOT_PATH . '/lib/models/surveys/Answers.php';//Chinh Nguyen add

require_once ROOT_PATH . '/lib/models/surveys/Options.php';//Chinh Nguyen add
require_once ROOT_PATH . '/lib/models/surveys/Questions.php';//Chinh Nguyen add
require_once ROOT_PATH . '/lib/models/surveys/Surveys.php';//Chinh Nguyen add

require_once ROOT_PATH . '/lib/models/hrfunct/EmpInfo.php';

class MailSurvey{
	
	private $templateFile;
	private $to;
	private $from;
	private $mail;
	private $mailer;
	private $subject;
	
	public function getMailTo(){
		return $this->to;
	} 
	
	public function setMailTo($mailTo){
		$this->to = $mailTo;
	}
	
	public function getMailFrom(){
		return $this->from;	
	}
	
	public function setMailFrom($mailFrom){
		$this->from = $mailFrom;
	}
	
	public function getMail(){
		return $this->mail;
	} 
	
	public function setMail($mail){
		$this->mail = $mail;
	}
	
	public function getMailSubject(){
		return $this->subject;
	} 
	
	public function setMailSubject($mailSubject){
		$this->subject= $mailSubject;
	}
	public function __construct() {
		$confObj = new EmailConfiguration();

		$this->mailer = new htmlMimeMail5();
		$auth=true;
		if ($confObj->getSmtpUser() == '') {
			$auth=false;
		}
		
		$this->mailer->setTextCharset('utf-8');
        $this->mailer->setHtmlCharset('utf-8');
        $this->mailer->setHeadCharset('utf-8');
	
		$this->mailer->setSMTPParams($confObj->getSmtpHost(), $confObj->getSmtpPort(), null, $auth, $confObj->getSmtpUser(), $confObj->getSmtpPass());

		$this->mailer->setSendmailPath($confObj->getSendmailPath());

		$this->mailer->setFrom("Human Resources <{$confObj->getMailAddress()}>");

		$sysConfObj = new sysConf();

		$this->employeeIdLength = $sysConfObj->getEmployeeIdLength();

		$this->mailType = $confObj->getMailType();
		
	}
	
	public function __destruct() {
		//nothing to do
	}

	public function send() {
		
	return $this->_sendMail();
		
	}
	
	private function _sendMail() {
		//$this->_buildMail();

		$mailer = $this->mailer;
		$mailer->setHTML($this->mail);
		$mailer->setFrom($this->from);
		$mailer->setSubject($this->subject);
		$logMessage = date('r')." Sending {$this->subject} to";
		if (isset($this->to) && is_array($this->to)) {
			foreach ($this->to as $to) {
				$logMessage .= "\r\n".$to;
				//error_log($logMessage,3,"C:/logtest.log");
			}
		}
		if ((!is_array($this->to)) || (!@$mailer->send($this->to, $this->mailType))) {

			$logMessage .= " - FAILED \r\nReason(s):";
			if (isset($mailer->errors)) {
				$logMessage .= "\r\n\t*\t".implode("\r\n\t*\t",$mailer->errors);
			}
		} else {
			$logMessage .= " - SUCCEEDED";
		}

		if (isset($confObj->logPath) && !empty($confObj->logPath)) {
			$logPath = $confObj->logPath;
		} else {
			$logPath = ROOT_PATH.'/lib/logs/';
		}

		error_log($logMessage."\r\n", 3, $logPath."notification_mails.log");

		return true;
	}
	

}



?>